package com.reptile.model.title.service.impl;

import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.db.DBSql;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.reptile.model.title.model.TitleModel;
import com.reptile.model.title.model.TitleModelExt;
import com.reptile.model.title.service.TitleModelService;

/**
 * @Classname TitleModelServiceImpl
 * @Description TODO
 * @Date 2022/8/7 1:05
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class TitleModelServiceImpl implements TitleModelService {

    @Autowired
    DBSql dbSql;

    private static final String TITLE_TABLE_NAME=" select a.id,a.title,a.contentid,a.tag,a.ismarker,a.channelId,b.channelName,a.uid,c.realname,a.createdate from " +
            " sys_title a left join sys_channel b on a.channelId=b.id left join sys_user c on a.uid=c.id ";

    private static final String TITLE_CONTENT_TABLE_NAME=" select a.id,a.title,a.contentid,a.tag,a.ismarker,a.channelId,b.channelName,a.uid,c.realname,a.createdate,d.content " +
            " from sys_title a left join sys_channel b on a.channelId=b.id left join sys_user c on a.uid=c.id left join sys_content d on a.contentid=d.id ";

    @Autowired
    TableService tableService;



    @Override
    public TitleModel save(TitleModel titleModel) {
        tableService.save(titleModel,TitleModel.class);
        return titleModel;
    }

    @Override
    public TitleModel update(TitleModel titleModel) {
        tableService.updateById(titleModel,TitleModel.class);
        return titleModel;
    }

    @Override
    public TitleModel getId(Object id) {
        TitleModel titleModel=tableService.getOneById(id,TitleModel.class);
        return titleModel;
    }

    @Override
    public TitleModelExt getTitleModelExtById(Object id) {
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.select("*").from("("+TITLE_CONTENT_TABLE_NAME+") a");
        sqlBuilder.and("id").eq(id);
        TitleModelExt titleModelExt=dbSql.selectMapObj(sqlBuilder.getSql(),TitleModelExt.class);
        return titleModelExt;
    }
}
